package com.deve.pig.service.customer.impl;

import com.deve.pig.common.BusinessData;
import com.deve.pig.mapper.CustomerMapper;
import com.deve.pig.model.Customer;
import com.deve.pig.model.CustomerExample.Criteria;
import com.deve.pig.model.CustomerExample;
import com.deve.pig.service.customer.ICustomerService;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import my.utils.Pagination;
import org.springframework.stereotype.Service;

@Service("customerService")
public class CustomerServiceImpl extends BusinessData implements ICustomerService {
    @Resource
    private CustomerMapper customerMapper;

    @Override
    public int add(Customer entity) {
        return customerMapper.insert(entity);
    }

    @Override
    public int delete(CustomerExample example) {
        return customerMapper.deleteByExample(example);
    }

    @Override
    public int delete(int id) {
        return customerMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Customer entity) {
        return customerMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public Pagination findPagination(Map<String, Object> queryMap, int currentPage, int pageSize) {
        CustomerExample customerExample = new CustomerExample();
        //Criteria criteria = customerExample.createCriteria();
        // 设置搜索条件参数
        //if(queryMap != null){
            //if(queryMap.containsKey("username")) {
                //criteria.andUserNameLike("%"+(String)queryMap.get("username")+"%");
                //}
                //if(queryMap.containsKey("email")){
                    //criteria.andEmailLike((String)queryMap.get("email"));
                    //}
                    //}
                    // 设置分页参数
                    customerExample.setPageSize(pageSize);
                    customerExample.setStartIndex((currentPage-1)*pageSize);
                    List<Customer> items = customerMapper.selectByExample(customerExample);
                    int totalCount = (int)customerMapper.countByExample(customerExample);
                    return new Pagination(pageSize, currentPage, totalCount, items);
                }

    @Override
    public Customer findById(int id) {
        return customerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Customer> findAll() {
        return customerMapper.selectByExample(null);
    }

    @Override
    public List<Customer> findByCriteria(CustomerExample criteria) {
        return customerMapper.selectByExample(criteria);
    }

    @Override
    @SuppressWarnings("unchecked")
    public LinkedHashMap<String, List> getExportData() {
        LinkedHashMap<String, List> map = new LinkedHashMap<String, List>();
        map.put("SheetName_1", findAll());
        map.put("SheetName_2", findAll());
        map.put("SheetName_3", findAll());
        map.put("SheetName_N", findAll());
        return map;
    }

    @Override
    public int countByExample(CustomerExample example) {
        return customerMapper.countByExample(example);
    }
}